<?php
namespace app\modules\controllers;
use Yii;
use yii\web\Controller;
use app\modules\models\Admin;
use yii\data\Pagination;
class ManageController extends Controller
{
    //用于发送邮件中的修改密码
    public function actionMailchangepass()
    {
        $this->layout = false;
        $time = Yii::$app->request->get("timestamp");
        $adminuser = Yii::$app->request->get("adminuser");
        $token = Yii::$app->request->get("token");
        $model = new Admin;
        $myToken = $model->createToken($adminuser, $time);
        //验证是否是本人
        if ($token != $myToken) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        //超时返回登录
        if (time() - $time > 300) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        if(Yii::$app->request->isPost){
            $post= Yii::$app->request->post();
            if($model->changePass($post)){
                Yii::$app->session->setFlash('info','密码修改成功');
            }
        }
        $model->adminuser=$adminuser;
        return $this->render('mailchangepass',['model'=>$model]);
    }
    
    //管理员列表
    public function actionManages()
    {
        $this->layout='adminlist.php';
//        $managers= Admin::find()->all();
        $model= Admin::find();
        $count=$model->count();
        $pageSize= \Yii::$app->params['pageSize']['manage'];//从配置文件中获取
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $managers=$model->offset($pager->offset)->limit($pager->limit)->all();
        return $this->render('manages',['managers'=>$managers,'pager'=>$pager]);
    }
    
    //添加管理员
    public function actionReg()
    {
        $this->layout='adminlist.php';
        $model=new Admin();
        if(\Yii::$app->request->isPost)
        {
            $post= \Yii::$app->request->post();
            if($model->reg($post)){
                \Yii::$app->session->setFlash('info','添加成功');
            }else{
                \Yii::$app->session->setFlash('info','添加失败');
            }
        }
        $model->adminpass="";
        $model->respass="";
        return $this->render('reg',['model'=>$model]);
    }
    
    public function actionDel()
    {
        $adminid= \Yii::$app->request->get('adminid');
        if(empty($adminid)){
            $this->redirect(['manage/manages']);
        }
        $model=new Admin();
        if($model->deleteAll('adminid=:id',[':id'=>$adminid])){
            \Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['manage/manages']);
        }
    }
    
    //修改邮箱
    public function actionChangeemail()
    {
        $this->layout='adminlist.php';
        $model=new Admin();
//        var_dump(Yii::$app->session['admin']['adminuser']);exit;
        $data=$model->find()->where('adminuser=:user',[':user'=> Yii::$app->session['admin']['adminuser']])->one();
        if(Yii::$app->request->isPost){
            $post= Yii::$app->request->post();
            if($model->changeemail($post)){
            \Yii::$app->session->setFlash('info','修改成功');
            }else{
                 Yii::$app->session->setFlash('info','修改失败');
            }
        }
        $data->adminpass="";
        return $this->render('changeemail',['model'=>$data]);
    }
    
    //修改密码
    public function actionChangepass()
    {
        $this->layout='adminlist.php';
        $model= Admin::find()->where('adminuser=:user',[':user'=> Yii::$app->session['admin']['adminuser']])->one();
        if(Yii::$app->request->isPost){
            $post= Yii::$app->request->post();
            if($model->changePass($post)){
            \Yii::$app->session->setFlash('info','修改成功');
            }else{
                 Yii::$app->session->setFlash('info','修改失败');
            }
        }
        $model->adminpass="";
        $model->respass="";
        return $this->render('changepass',['model'=>$model]);
    }
}

